// export const URL = 'https://www.shlxkj.info/API/api'
 // export const URL = 'http://192.168.3.12:8010/api' 
 export const URL ='https://www.shlxkj.info:44320/API/api'   
import store from '@/store'
const params = (url, data) => {
	url += '?'
	const keys = Object.keys(data)
	for (const key of keys) {
		url += `${key}=${encodeURIComponent(typeof data[key] === 'string' ? data[key].trim() : data[key])}&`
	}
	return url.substring(0, url.length - 1)
}

const request = ({ method, url, data = {} }) => {
	return new Promise((resolve, reject) => {
		console.log('url: ', params(url, data))
		uni.request({
			url: method === 'GET' ? params(url, data) : url,
			method,
			data,
			header: {
				Authorization: 'Bearer ' + store.state.vx_token,
				ApiKey: store.state.vx_token
			},
			success: (res) => {
				if (res.statusCode === 200) {
					if (res.data.Code !== 200) {
						let pages = getCurrentPages()
						let current = pages[pages.length - 1]
						if (current.route === 'pages/login/index' || current.route === 'pages/home/index') {
							return resolve(res.data)
						}
						reject(res.data)
					}
					resolve(res.data ? res.data : res)
				} else if (res.statusCode === 401) {
					store.commit('$uStore', { name: 'vx_token', value: '' })
					let pages = getCurrentPages()
					let current = pages[pages.length - 1]
					if (current.route === 'pages/login/index' || current.route === 'pages/home/index') {
						return resolve(res.data)
					}
					uni.showToast({ title: '需重新登录' })
					uni.navigateTo({ url: '/pages/login/index' })
				} else {
					uni.showToast({ title: res.data.msg })
					reject(res.data)
				}
				const data = res.data

				if (data) {
					resolve(data)
				} else {
					reject(res)
				}
			},
			fail: (err) => {
				if (err.statusCode === 401) {
					uni.redirectTo({ url: '/pages/login/index' })
				}
				reject(err)
			}
		})
	})
}

// 登录
export function login (data) {
	return request({
		method: 'GET',
		url: `${URL}/WeChat/GetLogin`,
		data
	})
}

// 获取手机号码
export function getPhone (data) {
	return request({
		method: 'POST',
		url: `${URL}/Wechat/decryptData`,
		data
		}
	)
}

// 注册
export function register (data) {
	return request({
		method: 'POST',
		url: `${URL}/Wechat/Login`,
		data
	})
}

// 用户协议
export function getDictionaryByType (data) {
	return request({
		method: 'GET',
		url: `${URL}/Dictionary/GetDictionaryByType`,
		data
	})
}

// 解密
export function AESDecrypt (data) {
	return request({
		method: 'GET',
		url: `${URL}/WeChat/AESDecrypt`,
		data
	})
}

// 加密
export function AESEncrypt (data) {
	return request({
		method: 'GET',
		url: `${URL}/WeChat/AESEncrypt`,
		data
	})
}

// 初次上报（用户信息）
export function CreateReagentResult (data) {
	return request({
		method: 'POST',
		url: `${URL}/ReagentResult/CreateReagentResult`,
		data
	})
}
//二次上报（结果）
export function UpReagentResult (data) {
	return request({
		method: 'POST',
		url: `${URL}/ReagentResult/UpReagentResult`,
		data
	})
}

// 援助
export function UpdateReagentResult (data) {
	return request({
		method: 'PUT',
		url: `${URL}/ReagentResult/UpdateReagentResult`,
		data
	})
}

// 获取省市区
export function ConvertCoordinate (data) {
	return request({
		method: 'GET',
		url: `${URL}/WeChat/ConvertCoordinate`,
		data
	})
}

// 扫码领取电子检测盒
export function Shipment (data) {
	return request({
		method: 'GET',
		url: `${URL}/SelfService/Shipment`,
		data
	})
}

// 获取疾控中心
export function GetCDCVicinity (data) {
	return request({
		method: 'GET',
		url: `${URL}/CDC/GetCDCVicinity`,
		data
	})
}

// 报告过程数据
export function ReportedData (data) {
	return request({
		method: 'POST',
		url: `${URL}/WeChat/ReportedData`,
		data
	})
}
//Base64解码
export function Base64 (data) {
	return request({
		method: 'GET',
		url: `${URL}/Decrypt/decryptMac`,
		data
	})
}

//查询是否存在关联
export function GetReleted(data) {
	return request({
		method: 'GET',
		url: `${URL}/MacPhone/GetReleted`,
		data
	})
}

//新建手机号与设备号关联
export function CreateReleted(data) {
	return request({
		method: 'POST',
		url: `${URL}/MacPhone/CreateReleted`,
		data
	})
}

//9006解密
export function aesDecrypt2(data) {
	return request({
		method: 'GET',
		url: `${URL}/Decrypt/decryptYuzhi`,
		data
	})
}
//9005解密结果值
export function aesDecryptjieguozhi(data) {
	return request({
		method: 'GET',
		url: `${URL}/Decrypt/decryptJieguozhi`,
		data
	})
}
//获取接口阈值
export function aesDecryptGetAPiyuzhi(data) {
	return request({
		method: 'GET',  
		url: `${URL}/Devicenoyuzhi/getYuzhiBy`,  
		data
	})
}

//查询是已有结果
export function Getresult(data) {
	return request({
		method: 'GET',
		url: `${URL}/ReagentResult/GetReagentResultInfo`,
		data
	})
}

//健康档案检测结果查询
export function Find_Releted(data) {
	return request({
		method: 'POST',
		url: `${URL}/ReagentResult/GetMacDirectionByPhone`,
		data
	})
}

//健康档案检测结果详情
export function Find_Releted_detail(data) {
	return request({
		method: 'POST',
		url:`${URL}/ReagentResult/GetMacDirectionInfo?reagentResultId=${data}`,
		data
	})
}

//健康档案科普知识
export function Kepu(data) {
	return request({
		method: 'POST',
		url: `${URL}/Knowledge/GetKnowledgeList`,
		data
	})
}

// 健康档案咨询问题模糊查询
export function Inquire(data){
	return request({
		method: 'POST',
		url: `${URL}/Problem/FuzzyFindByProblem`,
		data
	})
}

// 健康档案咨询根据问题Id查找答案
export function FindAnswer(id){
	return request({
		method: 'POST',
		url: `${URL}/Answer/FindAnswerByProblemId?problemid=${id}`,
	})
}

// 健康档案分页查询留言页面的数据
export function getMessageList(data){
	return request({
		method: 'POST',
		url: `${URL}/Leavemessage/PaginFindLeavemessage`,
		data
	})
}

// 健康档案发送留言
export function sendMessage(data){
	return request({
		method: 'POST',
		url: `${URL}/Leavemessage/InsertLeavemessage`,
		data
	})
}

//打卡接口
export function daka(data){
	return request({
		method: 'POST',
		url: `${URL}/Latestgps/CreateLatestgps`,
		data
	})
}

//上传文件
export function Upload(data){
	return request({
		method: 'POST',
		url: `${URL}/FileResoures/UploadFile`,
		data
	})
}

//上传小文件
export function Upload02(data){
	return request({
		method: 'POST',
		url: `${URL}/FileResoures/UploadFileByBase64`,
		data
	})
}
//创建普通检测记录
export function CreateGeneralDetection(data){
	return request({
		method: 'POST',
		url: `${URL}/GeneralDetection/CreateGeneralDetection`,
		data
	})
}

//发送验证码
export function SendCode(data) {
	return request({
		method: 'GET',
		url: `${URL}/Sms/SendCode`,
		data
	})
}

//验证并处理关系信息
export function DisposeUserrelationship(data1,data2,data3){
	return request({
		method: 'POST',
		url: `${URL}/Userrelationship/DisposeUserrelationship?userId=${data1}&type=${data2}&phone=${data3}`,
	})
}
//创建用户
export function CreateUser(data){
	return request({
		method: 'POST',
		url: `${URL}/User/CreateUser`,
		data
	})
}

//查询用户关系信息
export function FindUserrelationship(data){
	return request({
		method: 'POST',
		url: `${URL}/Userrelationship/FindUserrelationship`,
		data
	})
}

//分页查询普通检测记录
export function PagingFindGeneralDetection(data){
	return request({
		method: 'POST',
		url: `${URL}/GeneralDetection/PagingFindGeneralDetection`,
		data
	})
}

//查询普通检测详情
export function FindGeneralDetectionInfo(id) {
	return request({
		method: 'GET',
		url: `${URL}/GeneralDetection/FindGeneralDetectionInfo?id=${id}`,
	})
}

//OCR结果查询
export function OCR(id) {
	return request({
		method: 'GET',
		url: `${URL}/OCR/OCR?fileId=${id}`,
	})
}